package stackqueue;

import java.util.Stack;

/**
 * @Author: 海琳琦
 * @Date: 2022/2/7 17:04
 * 用两个栈实现队列
 */
public class MyQueue {

    private Stack<Integer> inst;

    private Stack<Integer> outst;

    public MyQueue() {
        inst = new Stack<>();
        outst = new Stack<>();
    }

    /**
     *  模拟入队列
     */
    public void push(int x) {
        inst.push(x);
    }


    /**
     *  模拟出队列
     */
    public int pop() {
        //输出栈为空，输入栈进输出栈
        if (outst.isEmpty()) {
            //输入栈不为空
            while (!inst.isEmpty()) {
                outst.push(inst.pop());
            }
        }
        return outst.pop();
    }

    /**
     * 模拟返回队头元素
     * @return
     */
    public int peek() {
        //输出栈为空，输入栈进输出栈
        if (outst.isEmpty()) {
            //输入栈不为空
            while (!inst.isEmpty()) {
                outst.push(inst.pop());
            }
        }
        return outst.peek();
    }

    /**
     * 判断队列是否为空
     * @return
     */
    public boolean empty() {
        return inst.isEmpty() && outst.isEmpty();
    }
}
